home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Snippets / ArraySpeed 1.0 / ArraySpeed.p < prev    next >
Encoding:
Text File  |  1995-12-30  |  6.5 KB  |  341 lines  |  [TEXT/PJMM]

  1. program ArraySpeed;
  2.  
  3. {$IFC UNDEFINED THINK_PASCAL}
  4.     uses
  5.         Types, ToolUtils;
  6. {$ENDC}
  7.  
  8.     const
  9.         kArrSize = 20000;
  10.         kNumLoops = 50;
  11.     type
  12.         IntPtr = ^Integer;
  13.     var
  14.         theByteArr: packed array[0..kArrSize] of Byte;
  15. {theIntArr: array[0..kArrSize] of Integer;}
  16.  
  17.     type
  18.         BytesArr = packed array[0..kArrSize] of Byte;
  19.         BytesPtr = ^BytesArr;
  20.  
  21.     var
  22.         i: Longint;
  23.         loop: Integer;
  24.         p: Ptr;
  25.         chp: BytesPtr;
  26.  
  27.         ip: IntPtr;
  28.         startTicks, finalTicks: Longint;
  29. begin
  30. {$IFC UNDEFINED THINK_PASCAL}
  31. {$ELSEC}
  32.     ShowText;
  33. {$ENDC}
  34.  
  35.     startTicks := TickCount;
  36.     for loop := 1 to kNumLoops do
  37.         for i := 0 to kArrSize do
  38.             theByteArr[i] := Byte(i);
  39.     finalTicks := TickCount;
  40.     WriteLn('for[]: ', finalTicks - startTicks : 1, ' ticks.');
  41.  
  42.     startTicks := TickCount;
  43.     for loop := 1 to kNumLoops do
  44.         begin
  45.             p := @theByteArr[0];
  46.             for i := 0 to kArrSize do
  47.                 begin
  48.                     p^ := i;
  49.                     p := Ptr(Longint(p) + 1);
  50.                 end;
  51.         end;
  52.     finalTicks := TickCount;
  53.     WriteLn('for ptr: ', finalTicks - startTicks : 1, ' ticks.');
  54.  
  55.     startTicks := TickCount;
  56.     for loop := 1 to kNumLoops do
  57.         begin
  58.             i := kArrSize;
  59.             while i > 0 do
  60.                 begin
  61.                     theByteArr[i] := i;
  62.                     i := i - 1;
  63.                 end;
  64.         end;
  65.     finalTicks := TickCount;
  66.     WriteLn('while []: ', finalTicks - startTicks : 1, ' ticks.');
  67.  
  68.     startTicks := TickCount;
  69.     for loop := 1 to kNumLoops do
  70.         begin
  71.             p := @theByteArr[0];
  72.             i := kArrSize;
  73.             while i > 0 do
  74.                 begin
  75.                     p^ := i;
  76.                     p := Ptr(Longint(p) + 1);
  77.                     i := i - 1;
  78.                 end;
  79.         end;
  80.     finalTicks := TickCount;
  81.     WriteLn('while ptr: ', finalTicks - startTicks : 1, ' ticks.');
  82.  
  83.     startTicks := TickCount;
  84.     for loop := 1 to kNumLoops do
  85.         begin
  86.             p := @theByteArr[0];
  87.             i := kArrSize;
  88.             while i > 2 do
  89.                 begin
  90.                     p^ := i;
  91.                     p := Ptr(Longint(p) + 1);
  92.                     i := i - 1;
  93.                     p^ := i;
  94.                     p := Ptr(Longint(p) + 1);
  95.                     i := i - 1;
  96.                 end;
  97.             while i > 0 do
  98.                 begin
  99.                     p^ := i;
  100.                     p := Ptr(Longint(p) + 1);
  101.                     i := i - 1;
  102.                 end;
  103.         end;
  104.     finalTicks := TickCount;
  105.     WriteLn('while ptr unroll 2: ', finalTicks - startTicks : 1, ' ticks.');
  106.  
  107.     startTicks := TickCount;
  108.     for loop := 1 to kNumLoops do
  109.         begin
  110.             p := @theByteArr[0];
  111.             i := kArrSize;
  112.             while i > 4 do
  113.                 begin
  114.                     p^ := i;
  115.                     p := Ptr(Longint(p) + 1);
  116.                     i := i - 1;
  117.                     p^ := i;
  118.                     p := Ptr(Longint(p) + 1);
  119.                     i := i - 1;
  120.                     p^ := i;
  121.                     p := Ptr(Longint(p) + 1);
  122.                     i := i - 1;
  123.                     p^ := i;
  124.                     p := Ptr(Longint(p) + 1);
  125.                     i := i - 1;
  126.                 end;
  127.             while i > 0 do
  128.                 begin
  129.                     p^ := i;
  130.                     p := Ptr(Longint(p) + 1);
  131.                     i := i - 1;
  132.                 end;
  133.         end;
  134.     finalTicks := TickCount;
  135.     WriteLn('while ptr unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
  136.  
  137.     startTicks := TickCount;
  138.     for loop := 1 to kNumLoops do
  139.         begin
  140.             p := @theByteArr[0];
  141.             i := kArrSize;
  142.             while i > 8 do
  143.                 begin
  144.                     p^ := i;
  145.                     p := Ptr(Longint(p) + 1);
  146.                     i := i - 1;
  147.                     p^ := i;
  148.                     p := Ptr(Longint(p) + 1);
  149.                     i := i - 1;
  150.                     p^ := i;
  151.                     p := Ptr(Longint(p) + 1);
  152.                     i := i - 1;
  153.                     p^ := i;
  154.                     p := Ptr(Longint(p) + 1);
  155.                     i := i - 1;
  156.                     p^ := i;
  157.                     p := Ptr(Longint(p) + 1);
  158.                     i := i - 1;
  159.                     p^ := i;
  160.                     p := Ptr(Longint(p) + 1);
  161.                     i := i - 1;
  162.                     p^ := i;
  163.                     p := Ptr(Longint(p) + 1);
  164.                     i := i - 1;
  165.                     p^ := i;
  166.                     p := Ptr(Longint(p) + 1);
  167.                     i := i - 1;
  168.                 end;
  169.             while i > 0 do
  170.                 begin
  171.                     p^ := i;
  172.                     p := Ptr(Longint(p) + 1);
  173.                     i := i - 1;
  174.                 end;
  175.         end;
  176.     finalTicks := TickCount;
  177.     WriteLn('while ptr unroll 8: ', finalTicks - startTicks : 1, ' ticks.');
  178.  
  179.     startTicks := TickCount;
  180.     for loop := 1 to kNumLoops do
  181.         begin
  182.             p := @theByteArr[0];
  183.             i := kArrSize;
  184.             while i > 16 do
  185.                 begin
  186.                     p^ := i;
  187.                     p := Ptr(Longint(p) + 1);
  188.                     i := i - 1;
  189.                     p^ := i;
  190.                     p := Ptr(Longint(p) + 1);
  191.                     i := i - 1;
  192.                     p^ := i;
  193.                     p := Ptr(Longint(p) + 1);
  194.                     i := i - 1;
  195.                     p^ := i;
  196.                     p := Ptr(Longint(p) + 1);
  197.                     i := i - 1;
  198.                     p^ := i;
  199.                     p := Ptr(Longint(p) + 1);
  200.                     i := i - 1;
  201.                     p^ := i;
  202.                     p := Ptr(Longint(p) + 1);
  203.                     i := i - 1;
  204.                     p^ := i;
  205.                     p := Ptr(Longint(p) + 1);
  206.                     i := i - 1;
  207.                     p^ := i;
  208.                     p := Ptr(Longint(p) + 1);
  209.                     i := i - 1;
  210.                     p^ := i;
  211.                     p := Ptr(Longint(p) + 1);
  212.                     i := i - 1;
  213.                     p^ := i;
  214.                     p := Ptr(Longint(p) + 1);
  215.                     i := i - 1;
  216.                     p^ := i;
  217.                     p := Ptr(Longint(p) + 1);
  218.                     i := i - 1;
  219.                     p^ := i;
  220.                     p := Ptr(Longint(p) + 1);
  221.                     i := i - 1;
  222.                     p^ := i;
  223.                     p := Ptr(Longint(p) + 1);
  224.                     i := i - 1;
  225.                     p^ := i;
  226.                     p := Ptr(Longint(p) + 1);
  227.                     i := i - 1;
  228.                     p^ := i;
  229.                     p := Ptr(Longint(p) + 1);
  230.                     i := i - 1;
  231.                     p^ := i;
  232.                     p := Ptr(Longint(p) + 1);
  233.                     i := i - 1;
  234.                 end;
  235.             while i > 0 do
  236.                 begin
  237.                     p^ := i;
  238.                     p := Ptr(Longint(p) + 1);
  239.                     i := i - 1;
  240.                 end;
  241.         end;
  242.     finalTicks := TickCount;
  243.     WriteLn('while ptr unroll 16: ', finalTicks - startTicks : 1, ' ticks.');
  244.  
  245.     startTicks := TickCount;
  246.     for loop := 1 to kNumLoops do
  247.         begin
  248.             i := kArrSize;
  249.             while i > 4 do
  250.                 begin
  251.                     theByteArr[i] := i;
  252.                     i := i - 1;
  253.                     theByteArr[i] := i;
  254.                     i := i - 1;
  255.                     theByteArr[i] := i;
  256.                     i := i - 1;
  257.                     theByteArr[i] := i;
  258.                     i := i - 1;
  259.                 end;
  260.             while i > 0 do
  261.                 begin
  262.                     theByteArr[i] := i;
  263.                     i := i - 1;
  264.                 end;
  265.         end;
  266.     finalTicks := TickCount;
  267.     WriteLn('while [] unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
  268.  
  269.     startTicks := TickCount;
  270.     for loop := 1 to kNumLoops do
  271.         begin
  272.             i := kArrSize;
  273.             while i > 8 do
  274.                 begin
  275.                     theByteArr[i] := i;
  276.                     i := i - 1;
  277.                     theByteArr[i] := i;
  278.                     i := i - 1;
  279.                     theByteArr[i] := i;
  280.                     i := i - 1;
  281.                     theByteArr[i] := i;
  282.                     i := i - 1;
  283.                     theByteArr[i] := i;
  284.                     i := i - 1;
  285.                     theByteArr[i] := i;
  286.                     i := i - 1;
  287.                     theByteArr[i] := i;
  288.                     i := i - 1;
  289.                     theByteArr[i] := i;
  290.                     i := i - 1;
  291.                 end;
  292.             while i > 0 do
  293.                 begin
  294.                     theByteArr[i] := i;
  295.                     i := i - 1;
  296.                 end;
  297.         end;
  298.     finalTicks := TickCount;
  299.     WriteLn('while [] unroll 8: ', finalTicks - startTicks : 1, ' ticks.');
  300.  
  301.  
  302.  
  303.  
  304.  
  305.     startTicks := TickCount;
  306.     for loop := 1 to kNumLoops do
  307.         begin
  308.             chp := BytesPtr(@theByteArr[0]);
  309.             i := kArrSize;
  310.             while i > 4 do
  311.                 begin
  312.                     chp^[0] := i;
  313.                     chp := @chp^[1];
  314.                     i := i - 1;
  315.                     chp^[0] := i;
  316.                     chp := @chp^[1];
  317.                     i := i - 1;
  318.                     chp^[0] := i;
  319.                     chp := @chp^[1];
  320.                     i := i - 1;
  321.                     chp^[0] := i;
  322.                     chp := @chp^[1];
  323.                     i := i - 1;
  324.                 end;
  325.             while i > 0 do
  326.                 begin
  327.                     chp^[0] := i;
  328.                     chp := @chp^[1];
  329.                     i := i - 1;
  330.                 end;
  331.         end;
  332.     finalTicks := TickCount;
  333.     WriteLn('while ptr @[] unroll 4: ', finalTicks - startTicks : 1, ' ticks.');
  334.  
  335. {$IFC UNDEFINED THINK_PASCAL}
  336. {$ELSEC}
  337.     WriteLn('Click to exit.');
  338.     while not Button do
  339.         ;
  340. {$ENDC}
  341. end.